<template>
  <div>
    <van-nav-bar
      title="地址列表"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div class="main">
      <van-address-list
        @add="onAdd"
        v-model="curren"
        :list="list"
        default-tag-text="默认"
      />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      curren: "",
    };
  },
  mounted() {},
  created() {
    this.getAddress();
  },
  activated() {
    this.getAddress();
  },
  methods: {
    onAdd() {
      this.$router.push("/addAddress");
    },
    async getAddress() {
      const { data: res } = await this.$http.get("address", {
        params: { uid: this.$store.state.emailLogin[0].userID },
      });
      if (res.status == 200) {
        res.data.forEach((item) => {
          item.isDefault = item.isDefault == 0 ? false : true;
        });
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].isDefault === true) {
            this.curren = res.data[i].id;
          }
        }
        this.list = res.data;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.main {
  position: absolute;
  top: 45px;
  width: 100%;
  left: 0;
  background-color: #f4f4f4;
  /* height: 100vh; */
  bottom: 0;
}
</style>